<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>档案管理系统</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <jsp:include page="/inc/head.jsp" flush="true"/>
    <jsp:include page="/inc/footer.jsp" flush="true"/>
    <script src="${basePath }/js/tip.js"></script>
</head>
<body class="hold-transition skin-blue sidebar-mini" >
<input type="hidden" id="userId" name="userId" value="" >
<!-- Main content -->
<section class="content">
    <!-- /rowToolBar -->
    <div id = "rowToolBar" class="row" style="height: 100%;">
        <div class="col-xs-12">
            <div class="box box-info">
                <form class="form-horizontal">
                    <!-- /.box-header -->
                    <div class="box-body">
                        <div class="form-group" style="float: left ;margin-right: 50px;margin-top: 15px;margin-bottom: 5px">
                            <button type="button" onclick="addUser()" class="btn btn-success">添加用户</button>
                        </div>
                        <div class="form-group" style="float: left; margin-right: 30px;margin-top: 15px;margin-bottom: 5px">
                            <input class="form-control" id="username" placeholder="请输入用户名称"  rows="3"/>
                        </div>
                        <!-- 地市选择 -->
                        <div class="form-group"style="float: left;margin-right: 30px;margin-top: 15px;margin-bottom: 5px">
                            <custom:screenDept test="1">
                                <select  class="form-control" id="chooseCity"  >
                                </select>
                            </custom:screenDept>
                        </div>
                        <div class="form-group" style="float: left;margin-top: 15px;margin-bottom: 5px">
                            <button type="button" onclick="searchUserAction()" class="btn btn-primary">搜索</button>
                        </div>
                       <%-- <div class="form-group" style="float: left;margin-top: 15px;margin-bottom: 5px">
                            <button type="button" onclick="AutoAction()" class="btn btn-primary">自动</button>
                        </div>--%>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id = "rowTable" class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body table-responsive no-padding">
                    <table id="table" class="table table-hover"></table>
                </div>
            </div>
        </div>
    </div>
    <jsp:include page="/resource/addUser.jsp" flush="true"/>
</section>

<script>
    var $table = $('#table');
    $('[name="status"]').bootstrapSwitch({
        onText:"开",
        offText:"关",
        onColor:"success",
        offColor:"info",
        size:"null",
        onSwitchChange:function(event,state){
            if(state==true){
                $(this).val("1");
            }else{
                $(this).val("0");
            }
        }
    })
    $(function () {
        //Date picker
        $('#datepicker1').datepicker()
        $('#datepicker2').datepicker({
            autoclose: true
        })
        var url = '${basePath}/pcwindow/findAllUser';
        userManagerAction(url);
        loadAllCity();
    });
    function userManagerAction(urla){
            $table.bootstrapTable({
                url:urla,
                striped: true,
                //search: false,
                //showRefresh: false,
                //showColumns: false,
                minimumCountColumns: 2,
                clickToSelect: true,//是否启用点击选中行
                singleSelect : true,
                detailView: false,
                detailFormatter: 'detailFormatter',
                pagination: true,
                pageSize: 50,
                pageList: [50, 100],
                smartDisplay:false,
                paginationLoop: true,
                sidePagination: 'server',
                silentSort: false,
                escape: true,
                searchOnEnterKey: true,
                maintainSelected: true,
                columns: [
                    {field: 'ck', checkbox: true},
                    {field: 'user_name', title: '用户名',align: 'center'},
                    {field: 'user_createTime', title: '创建时间',align: 'center',sortable: true,formatter: 'timeFormatter'},
                    {field: 'user_status', title: '状态' ,align: 'center', formatter: 'statusFormatter'},
                    {field: 'ro_name', title: '角色',align: 'center'},
                    {field: 'action', title: '操作',align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
                ]
            });
        }


    /**
     *	状态显示按钮
     */
    function statusFormatter(value, row, index) {
        if (value == 0) {
            return  '<font style="color:grey;">●禁用</font>'
        }
        if (value == 1) {
            return  '<font style="color:dodgerblue;">●</font>开启'
        }
    }
    /**
     *	格式化时间(格式YYYY-dd-MM HH:mm:ss)
     */
    function timeFormatter(value , row, index) {
        return formatshortDateTime(value);
    }
    /**
     *	操作按钮
     */
    function actionFormatter(value, row, index) {
        if(row.user_status == 0){
            return '<a style="color:#01579b;" href="javascript:void(0)" onclick="oprateSwithAction(' + row.user_id +',1)" >启用</a>&nbsp;&nbsp;&nbsp;' +
                '<a style="color:#01579b;" href="javascript:void(0)"  onclick="editUserAction('+row.user_id+')" >编辑</a>';
        }else{
            return '<a style="color:#01579b;" href="javascript:void(0)"  onclick="oprateSwithAction(' + row.user_id+',0)" >禁用</a>&nbsp;&nbsp;&nbsp;' +
                '<a style="color:#01579b;" href="javascript:void(0)"  onclick="editUserAction('+row.user_id+')" >编辑</a>';
        }
    }
    /**
     *	添加用户按钮
     */
    function addUser(){
        clearDataUser();
        //加载所有角色
        loadAllRole();
        loadAllParentManager();
        loadAllvehicleManager();
        $("#usernameError").hide();
        $("#userpasswordError").hide();
        $("#userpassword1Error").hide();
        // 加载addmodel
        $('#AddUserModal').modal({
            keyboard: true
        });

    }
    /**
     *	编辑用户按钮
     */
    function editUserAction(userId){
        clearDataUser();
        //加载所有角色
        loadAllRole();
        loadAllParentManager();
        loadAllvehicleManager();
        $("#usernameError").hide();
        $("#userpasswordError").hide();
        $("#userpassword1Error").hide();
        userMessageShow(userId);
    }
    function oprateSwithAction(userId,status) {
        var rows = $table.bootstrapTable('getSelections');
            oprateSwith(status);
            $.ajax({
                type: "POST",
                url: "/pcwindow/userStatuModified",
                dataType: "json",
                data: {
                    userStatus: status,
                    userId: userId
                },
                success: function (data) {
                    if (data.status == 200) {
                        $("#table").bootstrapTable('refresh');
                    }
                }
            })
    }
    function oprateSwith(status){
        var text;
        if(status == 1){
            text = "开启"
        }else{
            text = "禁用"
        }
        Ewin.confirm({message: '确认要'+text+'这个用户吗？'}).on(function (e) {
            if (!e) {
                return;
            }
        });
    }
    /**
     *	加载所有角色
     */
    function loadAllRole(){
        $.ajax({
            type: "POST",
            url:"${pageContext.request.contextPath }/pcwindow/findAllRole",
            dataType:"json",
            async:false,
            success: function(data){
                if (data.length != 0) {
                    var Role = data.rows;
                   var str;
                   for(var i = 0;i < Role.length ;i++){
                       var roname=Role[i].roName;
                       var roid=Role[i].roId;
                       str += '<option value="'+ roid +'">'+ roname +'</option>'
                    }
                    $('#addRoleList').html(str);
                }
            }
        });
    }
    /**
     *	加载所有支队
     */
   function loadAllParentManager(){
        $.ajax({
            type: "POST",
            url:"${pageContext.request.contextPath }/pcwindow/findAllParentManager",
            dataType:"json",
            async:false,
            success: function(data){
                if (data.length > 0) {
                    var ManagerDepartment = data;
                    var str =  '<option value="0">请选择支队信息</option>';
                    for(var i = 0;i < ManagerDepartment.length ;i++){
                        var mdName=ManagerDepartment[i].mdName;
                        var mdCode=ManagerDepartment[i].mdCode;
                        str += '<option value="'+ mdCode +'">'+ mdName +'</option>'
                    }
                    $('#addParentManList').html(str);
                }
            }
        });
    }
    /**
     *	加载所有车管所
     */
    function loadAllvehicleManager(){
        $.ajax({
            type: "POST",
            url:"${pageContext.request.contextPath }/pcwindow/findAllVehicleManage",
            dataType:"json",
            async:false,
            success: function(data){
                if (data.length > 0) {
                    var ManagerDepartment = data;
                    var str =  '<option value="0">请选择管理部门信息</option>';
                    for(var i = 0;i < ManagerDepartment.length ;i++){
                        var mdName=ManagerDepartment[i].mdName;
                        var mdCode=ManagerDepartment[i].mdCode;
                        str += '<option value="'+ mdCode +'">'+ mdName +'</option>';
                    }
                    $('#addVehicleList').html(str);
                }
            }
        });
    }
    /**
     *	加载所有地市
     */
    function loadAllCity(){
        $.ajax({
            type: "POST",
            url:"${pageContext.request.contextPath }/pcwindow/getCitys",
            dataType:"json",
            async:false,
            success: function(data){
                if (data.length > 0) {
                    var str =  '<option value="">请选择地市</option>';
                    $.each(data,function(i,val){
                        str += '<option value="'+ val +'">'+ val +'</option>';
                    })
                    $('#chooseCity').html(str);
                }
            }
        });
    }
    function searchUserAction() {
        var userName=$("#username").val();
        var cityName=$("#chooseCity").find(":selected").val();
        /*$.ajax({
            type: "POST",
            url:"",
            dataType:"json",
            data:{
                userName:userName,
                cityName:cityName
            },
            async:false,
            success: function(data){
                    $('#table').bootstrapTable('load',data);
            }
        });*/
        $table.bootstrapTable('destroy');
        userManagerAction("${pageContext.request.contextPath }/pcwindow/findUserByTerm?userName="+userName+"&cityName="+cityName);
    }
    function clearDataUser() {
        $("#userName").val("");
        $("#password").val("");
        $("#repassword").val("");
        $("#userIdCode").val("");
        $("#userPeName").val("");
        $("#allManager").prop('checked',false);
        $("#userStatus").prop('checked',false);
    }
    function AutoAction() {
        $.ajax({
            type: "POST",
            url:"${pageContext.request.contextPath }/pcwindow/autoActionTest",
            dataType:"json",
            success: function(data){
            }
        });
    }
</script>
</body>
</html>

